package com.liu.SocketTcp;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * 客户端
 * 字符流
 */
public class SocketTcp02 {
    public static void main(String[] args) throws IOException {
        //创建一个Socket对象拿到需要的IP或者域名，这里是本机的信息，加入所需要的端口号
        Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
        //通过转化流将socket.getOutputStream()转成BufferedWriter字符处理流
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        //将传出信息Write
        bufferedWriter.write("Hello server");
        //newline表示换行告诉服务器之后不再传入
        bufferedWriter.newLine();
        //flush需要手动刷新否则传不出去
        bufferedWriter.flush();


        //将需要传入的数据转化为字符
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //用readLine接收newLine信息
        String s = bufferedReader.readLine();
        System.out.println(s);


        bufferedReader.close();
        bufferedWriter.close();
        socket.close();
    }
}
